/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerDesk.java
 *
 * Created on 2011-5-13, 16:20:16
 */
package starcup.ui.desk;

import starcup.core.game.Player;
import starcup.ui.card.JCard;

/**
 *
 * @author Akira.Pan
 */
public class PlayerDesk extends javax.swing.JPanel {

    private Player player;

    /** Creates new form PlayerDesk */
    public PlayerDesk() {
        initComponents();
    }

    /**刷新手牌的排列局部*/
    private void refleshCardLayout() {
        if (this.getPlayer().getHandCards().isEmpty()) {
            return;
        }
        int widthSpace = this.getWidth() / this.getPlayer().getHandCards().size();
        widthSpace = (widthSpace > JCard.CARD_WIDTH) ? JCard.CARD_WIDTH : widthSpace;
        this.handDeckPanel.removeAll();
        for (int i = 0; i < this.getPlayer().getHandCards().size(); i++) {
            final JCard card = (JCard) this.getPlayer().getHandCards().get(i);
            this.handDeckPanel.add(card, new org.netbeans.lib.awtextra.AbsoluteConstraints(i * widthSpace, 50, -1, -1));
            card.addMouseListener(new java.awt.event.MouseAdapter() {

                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    if (evt.getClickCount() == 1) {
                        if (card.isSeleceted()) {
                        } else {
                        }
                    } else if (evt.getClickCount() == 2) {
                        playCard(card);
                    }

                }
            });

        }
    }

    /**显示现有玩家的所有手牌，按照手牌的数量调增其间距 */
    public void displayCards() {
        this.refleshCardLayout();
        this.revalidate();
    }

    private void playCard(JCard card) {
        this.player.getHandCards().remove(card);
        this.displayCards();
        this.publicPanel.add(card);

    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cardImage = new javax.swing.JLabel();
        publicPanel = new javax.swing.JPanel();
        playerPanel = new javax.swing.JPanel();
        handDeckPanel = new javax.swing.JPanel();

        cardImage.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(starcup.StarCupApp.class).getContext().getResourceMap(PlayerDesk.class);
        cardImage.setIcon(resourceMap.getIcon("cardImage.icon")); // NOI18N
        cardImage.setText(resourceMap.getString("cardImage.text")); // NOI18N
        cardImage.setToolTipText(resourceMap.getString("cardImage.toolTipText")); // NOI18N
        cardImage.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        cardImage.setName("cardImage"); // NOI18N

        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(800, 600));
        setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        publicPanel.setBackground(resourceMap.getColor("publicPanel.background")); // NOI18N
        publicPanel.setMaximumSize(new java.awt.Dimension(800, 270));
        publicPanel.setMinimumSize(new java.awt.Dimension(800, 270));
        publicPanel.setName("publicPanel"); // NOI18N
        publicPanel.setPreferredSize(new java.awt.Dimension(800, 270));
        publicPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        add(publicPanel);

        playerPanel.setBackground(resourceMap.getColor("playerPanel.background")); // NOI18N
        playerPanel.setMaximumSize(new java.awt.Dimension(800, 300));
        playerPanel.setMinimumSize(new java.awt.Dimension(800, 300));
        playerPanel.setName("playerPanel"); // NOI18N
        playerPanel.setPreferredSize(new java.awt.Dimension(800, 300));
        playerPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        handDeckPanel.setMaximumSize(new java.awt.Dimension(600, 300));
        handDeckPanel.setMinimumSize(new java.awt.Dimension(600, 300));
        handDeckPanel.setName("handDeckPanel"); // NOI18N
        handDeckPanel.setPreferredSize(new java.awt.Dimension(600, 300));
        handDeckPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        playerPanel.add(handDeckPanel);

        add(playerPanel);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cardImage;
    private javax.swing.JPanel handDeckPanel;
    private javax.swing.JPanel playerPanel;
    private javax.swing.JPanel publicPanel;
    // End of variables declaration//GEN-END:variables
}
